const app = getApp();

Page({
  data: {
    posts: []
  },

  onLoad() {
    this.loadMyPosts();
  },

  onShow() {
    this.loadMyPosts();
  },

  async loadMyPosts() {
    try {
      const posts = await app.request(`/posts/user/${app.globalData.userInfo.id}`, 'GET');
      this.setData({ posts });
    } catch (error) {
      console.error('加载我的帖子失败:', error);
      wx.showToast({ title: '加载失败', icon: 'none' });
    }
  },

  onPostTap(e) {
    const postId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/post/detail/detail?id=${postId}`
    });
  },

  formatTime(dateString) {
    const date = new Date(dateString);
    return date.toLocaleDateString();
  }
});